

package com.click.app.webapp.action;

import com.click.app.DateFormatChanger;
import com.click.app.model.Laundry;
//import com.click.app.model.LaundryPayment;
import com.click.app.model.LaundryRequest;
import com.click.app.model.PeriodPaymentLaundry;
import com.click.app.service.LaundryManager;
import com.click.app.service.PeriodPaymentLaundryManager;
import com.click.app.webapp.reportObjects.RowEntry;
import com.click.app.webapp.reportObjects.Worksheet;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author ronald
 */
public class LaundryAction extends BaseAction {
    private List<Laundry> laundryList;
    private LaundryManager laundryManager;

    private Long id;
    private Laundry laundry;
    private List<PeriodPaymentLaundry> periodPaymentLaundryList;
    private String choicedPerPayLaundry;

    private PeriodPaymentLaundry periodPaymentLaundry;
    private PeriodPaymentLaundryManager periodPaymentLaundryManager;
    private Worksheet workSheet;
    private List<RowEntry> listRowEntries;
    private String dateSing;

    public String edit(){
        System.out.println("Laundry -> edit()");
        if(id != null ){
            laundry = laundryManager.get(id);
            dateSing = DateFormatChanger.changeDatetoString(laundry.getDateSing());
        }else
            laundry = new Laundry();

        System.out.println("Laundry -> end edit()");
        return SUCCESS;
    }
    public String delete(){
        System.out.println("Laundry -> delete()");
        laundryManager.remove(laundry.getId());
        saveMessage(getText("laundry.deleted"));
        System.out.println("Laundry -> end delete()");
        return "delete";
    }
    public String save(){
        System.out.println("Laundry -> save()"+dateSing);
        if(laundry.getName()!= null && laundry.getAddress()!= null &&
           !laundry.getName().equals("") && !laundry.getAddress().equals("") &&
            dateSing != null && !dateSing.equals("")){
            if(laundry.getId() != null){
                laundry.setDateSing(DateFormatChanger.changeStringtoDate(dateSing));
                laundryManager.save(laundry);
            }else{

                laundry.setDateSing(DateFormatChanger.changeStringtoDate(dateSing));
                laundry = laundryManager.save(laundry);

                periodPaymentLaundry = new PeriodPaymentLaundry();
                periodPaymentLaundry.setClosed(false);
                periodPaymentLaundry.setInitDate(laundry.getDateSing());
                periodPaymentLaundry.setInitAmount(0D);
                periodPaymentLaundry.setLaundry(laundry);
                periodPaymentLaundry.setDiscount(0.0D);
                periodPaymentLaundry.setCurrentAmount(0.0D);
                periodPaymentLaundry.setAmountPayment(0.0D);
                periodPaymentLaundry.setAmountWon(0.0D);
                periodPaymentLaundry.setEndAmount(0D);
                periodPaymentLaundryManager.save(periodPaymentLaundry);
                saveMessage(getText("periodPaymentLaundry.update"));
            }
            saveMessage(getText("laundry.update"));
        }else{
            id = laundry.getId();
            errorMessage(getText("laundry.error.fields"));
            edit();
            return ERROR;
        }
        System.out.println("Laundry -> end save()");
        return INPUT;
    }

    public String cancel(){
        System.out.println("Laundry -> cancel()");
        System.out.println("Laundry -> end cancel()");
        return INPUT;
    }
    
    public String list(){
        getSession().removeAttribute("lPid");
        laundryList = laundryManager.getAll();
        return SUCCESS;
    }

    public String choicedLaundry(){
        //if(id == null)
        //    id = (Long)getSession().getAttribute("lPid");

        if (id != null){
            laundry = laundryManager.get(id);
            periodPaymentLaundryList = laundry.getPeriodPaymentLaundryList();
        }
        return SUCCESS;
    }

    public String generateReport(){
//        if(!choicedPerPayLaundry.equals("-1")){
//           periodPaymentLaundry = periodPaymentLaundryManager.get(new Long(choicedPerPayLaundry));
//           List<RowEntry> listRowEntryEntry = getListRowEntryEntry(periodPaymentLaundry.getLaundryRequestList());
//           List<RowEntry> listRowEntryPayments = getListRowEntryPayment(periodPaymentLaundry.getLaundryPaymentList());
//           workSheet = new Worksheet(listRowEntryEntry, listRowEntryPayments);
//           workSheet.generateReport();
//           listRowEntries = workSheet.getRowEntrys();
//           laundry = laundryManager.get(laundry.getId());
//           periodPaymentLaundryList = laundry.getPeriodPaymentLaundryList();
//        }
        return "generateReportLaundry";
    }
    public List<RowEntry> getListRowEntryEntry(List<LaundryRequest> paymenRequestList){
        List<RowEntry> res = new ArrayList<RowEntry>();
        for (Iterator<LaundryRequest> it = paymenRequestList.iterator(); it.hasNext();) {
            LaundryRequest lr = it.next();
            RowEntry re = new RowEntry();
            re.setId(lr.getId());
            re.setTable("laundry_request");
            re.setEntry(lr.getTotalPrice());
            res.add(re);
        }
        return res;
    }
//    public List<RowEntry> getListRowEntryPayment(List<LaundryPayment> paymenLaundryList){
//        List<RowEntry> res = new ArrayList<RowEntry>();
//        for (Iterator<LaundryPayment> it = paymenLaundryList.iterator(); it.hasNext();) {
//            LaundryPayment lp = it.next();
//            RowEntry re = new RowEntry();
//            re.setId(lp.getId());
//            re.setTable("laundry_payment");
//            re.setPayment(lp.getAmount());
//            res.add(re);
//        }
//        return res;
//    }
    public List<Laundry> getLaundryList() {
        return laundryList;
    }

    public void setLaundryList(List<Laundry> laundryList) {
        this.laundryList = laundryList;
    }

    public LaundryManager getLaundryManager() {
        return laundryManager;
    }

    public void setLaundryManager(LaundryManager laundryManager) {
        this.laundryManager = laundryManager;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Laundry getLaundry() {
        return laundry;
    }

    public void setLaundry(Laundry laundry) {
        this.laundry = laundry;
    }

    public List<PeriodPaymentLaundry> getPeriodPaymentLaundryList() {
        return periodPaymentLaundryList;
    }

    public void setPeriodPaymentLaundryList(List<PeriodPaymentLaundry> periodPaymentLaundryList) {
        this.periodPaymentLaundryList = periodPaymentLaundryList;
    }

    public String getChoicedPerPayLaundry() {
        return choicedPerPayLaundry;
    }

    public void setChoicedPerPayLaundry(String choicedPerPayLaundry) {
        this.choicedPerPayLaundry = choicedPerPayLaundry;
    }

    public PeriodPaymentLaundry getPeriodPaymentLaundry() {
        return periodPaymentLaundry;
    }

    public void setPeriodPaymentLaundry(PeriodPaymentLaundry periodPaymentLaundry) {
        this.periodPaymentLaundry = periodPaymentLaundry;
    }

    public PeriodPaymentLaundryManager getPeriodPaymentLaundryManager() {
        return periodPaymentLaundryManager;
    }

    public void setPeriodPaymentLaundryManager(PeriodPaymentLaundryManager periodPaymentLaundryManager) {
        this.periodPaymentLaundryManager = periodPaymentLaundryManager;
    }

    public List<RowEntry> getListRowEntries() {
        return listRowEntries;
    }

    public void setListRowEntries(List<RowEntry> listRowEntries) {
        this.listRowEntries = listRowEntries;
    }

    public String getDateSing() {
        return dateSing;
    }

    public void setDateSing(String dateSing) {
        this.dateSing = dateSing;
    }
    
}
